function [ ret ] = hilbert_envelope_cc( dat1,dat2,freq_bands,fsample )

do_bandpass=1;
if nargin<3,
    do_bandpass=0;
    freq_bands={};
    freq_bands{1}=[0 0];
end;

ret=zeros(length(freq_bands),1);

    for ff=1:length(freq_bands),
        
        if do_bandpass,
            [tempdatbp1]=bandpass(dat1,freq_bands{ff},fsample,0);
            [tempdatbp2]=bandpass(dat2,freq_bands{ff},fsample,0);

        else
            tempdatbp1=dat1;
            tempdatbp2=dat2;
        end;
        
        signal_h = hilbert(tempdatbp1);

        % square the hilbert transform and take the
        % +ve square root to get the estimated envelope
        tempdatbp1 = (sqrt(signal_h.*conj(signal_h)))';
        
        signal_h = hilbert(tempdatbp2);

        % square the hilbert transform and take the
        % +ve square root to get the estimated envelope
        tempdatbp2 = (sqrt(signal_h.*conj(signal_h)))';


        cs = corrcoef(tempdatbp1,tempdatbp2);
        ret(ff) = cs(1,2);
    end;
    
end

